package mest.service.Modeling.Impl;

import com.alibaba.fastjson.JSON;
import mest.dao.Modeling.ICustomerTypeDao;
import mest.entity.customer_type;
import mest.service.Modeling.ICustomerTypeService;
import mest.util.ResultObj;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-13 21:46
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class CustomerTypeServiceImpl implements ICustomerTypeService {

    @Resource
    private ICustomerTypeDao ict;

    @Override
    public String insertCustomerType(customer_type ct,String theirDb) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            ict.insertCustomerType(ct,theirDb);
            map.put("code",0);
            map.put("msg","添加成功");
            return JSON.toJSONString(map);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")){
                map.put("msg","编码重复");
                return JSON.toJSONString(map);
            }
            map.put("msg","添加失败");
            return JSON.toJSONString(map);
        }
    }

    @Override
    public int selectCount(String theirDb) {
        return ict.selectCount(theirDb);
    }

    @Override
    public List<customer_type> findCustomerTypePage(int index, int pageSize,String theirDb) {
        return ict.findCustomerTypePage((index-1)*pageSize,pageSize, theirDb);
    }

    @Override
    public int delModel(Integer id,String theirDb) {
        return ict.delModel(id, theirDb);
    }

    @Override
    public List<customer_type> queryAll(Integer index, Integer pageSize, String record,String theirDb) {
        return ict.queryAll((index-1)*pageSize,pageSize,record, theirDb);
    }

    @Override
    public int findCount(String record,String theirDb) {
        return ict.findCount(record, theirDb);
    }

    @Override
    public String updateByOne(customer_type record, String theirDb) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            ict.updateByPrimaryKeySelective(record, theirDb);
            map.put("code", 0);
            map.put("msg", "添加成功");
            return JSON.toJSONString(map);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                map.put("msg", "编码重复");
                return JSON.toJSONString(map);
            }
            map.put("msg", "修改失败");
            return JSON.toJSONString(map);
        }
    }
}
